﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.ComponentModel.DataAnnotations;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Annotations
{
    /// <summary>
    /// 车牌号码验证属性
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class PlateNumberAttribute : ValidationAttribute
    {
        /// <summary>
        /// 确定指定的值的对象是否有效。
        /// </summary>
        /// <param name="value">要验证的对象的值</param>
        /// <param name="validationContext">验证上下文</param>
        /// <returns>true 如果指定的值是否有效，则为否则为 false。</returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value.IsEmpty()) return ValidationResult.Success;
            var result = ValidateHelper.IsPlateNumber(value.ToStringOrEmpty());
            return result ? ValidationResult.Success : new ValidationResult(ErrorMessage?? "请输入正确的车牌号码");
        }
    }
}